﻿Imports System.Net
Imports EnterpriseDT 'free external DLL for FTP
Imports System.Web.HttpUtility
Imports System.Text.Encoding
Imports ibnCredentials 'my DLL for FTP credentials

Module Module1

    Public Sub FTPMkDir() 'eDTFTP dll for FTP create directory
        Dim FTPCreateDir As EnterpriseDT.Net.Ftp.FTPConnection = New EnterpriseDT.Net.Ftp.FTPConnection
        'use external FTPCreds for logging in (protects my FTP account)
        FTPCreateDir.ServerAddress = FtpCreds.FTPaddress
        FTPCreateDir.UserName = FtpCreds.FTPusername
        FTPCreateDir.Password = FtpCreds.FTPpassword
        FTPCreateDir.CommandEncoding = GetEncoding("Windows-1252") 'preserves special characters during folder creation
        Try
            FTPCreateDir.Connect()
            FTPCreateDir.ChangeWorkingDirectory("new")
            FTPCreateDir.CreateDirectory(Form1.lstboxCast.SelectedItem.ToString.ToLower)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Public Sub FTPUpload(ByVal FTPName As String, ByVal LocalCopy As String)
        'easier to use this method instead of eDTFTP for upload with progress bar
        Dim clsRequest2 As FtpWebRequest = DirectCast(WebRequest.Create("ftp://projects.thehtpc.net/new/" & FTPName & "/folder.jpg"), FtpWebRequest)
        clsRequest2.KeepAlive = False
        Dim clsResponse2 As FtpWebResponse = Nothing
        clsRequest2.Credentials = New NetworkCredential(FtpCreds.FTPusername, FtpCreds.FTPpassword)
        clsRequest2.Method = WebRequestMethods.Ftp.UploadFile

        Dim bFile() As Byte = System.IO.File.ReadAllBytes(LocalCopy)
        Try
            Dim clsStream As System.IO.Stream = clsRequest2.GetRequestStream()
            Dim intChunk As Integer
            Dim intOffset As Integer
            intChunk = bFile.Length / 100
            Form1.pbUpload.Maximum = 100
            For i As Integer = 1 To 100
                If intOffset + intChunk > bFile.Length Then intChunk = bFile.Length - intOffset
                clsStream.Write(bFile, intOffset, intChunk)
                intOffset += intChunk
                Form1.pbUpload.Value = i 'update progress bar
            Next
            clsStream.Close()
            clsStream.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            If Not clsResponse2 Is Nothing Then
                clsResponse2.GetResponseStream().Close()
                clsResponse2.Close()
            End If
        End Try
        Form1.pbUpload.Value = 0 'reset progress bar when complete
    End Sub

    Public Sub FTPRenameDir() 'eDTFTP library for renaming a directory.  used for flagged actor items
        Dim FTPRename As EnterpriseDT.Net.Ftp.FTPConnection = New EnterpriseDT.Net.Ftp.FTPConnection
        'again, using FTPCreds for login
        FTPRename.ServerAddress = FtpCreds.FTPaddress
        FTPRename.UserName = FtpCreds.FTPusername
        FTPRename.Password = FtpCreds.FTPpassword
        FTPRename.CommandEncoding = GetEncoding("Windows-1252") 'preserve special characters
        Try
            FTPRename.Connect()
            FTPRename.ChangeWorkingDirectory("flagged")
            FTPRename.CreateDirectory(Form1.lstboxCast.SelectedItem.ToString.ToLower)
            FTPRename.ChangeWorkingDirectoryUp()
            FTPRename.RenameFile("old/" & Form1.lstboxCast.SelectedItem.ToString, "flagged/" & Form1.lstboxCast.SelectedItem.ToString)
            FTPRename.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Public Sub FTPRenameDir2() 'eDTFTP library for renaming a directory.  used for ez add actor items in this case
        Dim FTPRename As EnterpriseDT.Net.Ftp.FTPConnection = New EnterpriseDT.Net.Ftp.FTPConnection
        'ditto FTPCreds
        FTPRename.ServerAddress = FtpCreds.FTPaddress
        FTPRename.UserName = FtpCreds.FTPusername
        FTPRename.Password = FtpCreds.FTPpassword
        FTPRename.CommandEncoding = GetEncoding("Windows-1252") 'preserve special characters
        Try
            FTPRename.Connect()
            FTPRename.ChangeWorkingDirectory("new")
            FTPRename.CreateDirectory(Form1.lstboxCast.SelectedItem.ToString.ToLower)
            FTPRename.ChangeWorkingDirectoryUp()
            FTPRename.RenameFile("old/" & Form1.lstboxCast.SelectedItem.ToString, "new/" & Form1.lstboxCast.SelectedItem.ToString)
            FTPRename.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Module
